//
//  XGBaseStatusTableViewCell.swift
//  TodayNews
//
//  Created by monkey on 2020/2/24.
//  Copyright © 2020 itcast. All rights reserved.
//

import UIKit
import Kingfisher
import YYText

class XGBaseStatusTableViewCell: UITableViewCell,XGRegisterReusableViewProtocol 
{
    var statusViewModel: XGStatusViewModel? {
        didSet {
            if let userIcon = statusViewModel?.userIcon {
                userIconImageView.kf.setImage(with: URL(string: userIcon), options: [.processor(RoundCornerImageProcessor(cornerRadius: 20,targetSize: CGSize(width: 40, height: 40)))])
            }
            
            verifyImageView.isHidden = statusViewModel?.isUserVerified != true
            userNameLabel.text = statusViewModel?.userName
            createTimeLabel.text = statusViewModel?.createTime
            contentLabel.attributedText = statusViewModel?.contentAttributedString
            likeButton.setTitle(statusViewModel?.diggCount, for: .normal)
            commentButton.setTitle(statusViewModel?.commentCount, for: .normal)
            shareButton.setTitle(statusViewModel?.forwardCount, for: .normal)
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?)
    {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setUpUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: 私有属性
    
    /// 用户头像
    private lazy var userIconImageView = UIImageView()
    /// 用户名
    private lazy var userNameLabel = UILabel(text: "测试", textColor: UIColor.black, fontSize: 15, textAlignment: .left)
    /// 发布时间
    private lazy var createTimeLabel = UILabel(text: "测试", textColor: UIColor.darkGray, fontSize: 13, textAlignment: .left)
    /// 文字内容
    private(set) lazy var contentLabel: XGRichTextLabel = {
        let label = XGRichTextLabel()
        label.numberOfLines = 0
        label.preferredMaxLayoutWidth = TodayNews_Screen_Width - 20.0
        return label
    }()
    /// 点赞
    private lazy var likeButton = UIButton(title: "0", fontSize: 15, normalColor: UIColor.darkGray, imageName: "feed_like_24x24_")
    /// 评论
    private lazy var commentButton = UIButton(title: "0", fontSize: 15, normalColor: UIColor.darkGray, imageName: "comment_feed_24x24_")
    /// 转发分享
    private lazy var shareButton = UIButton(title: "0", fontSize: 15, normalColor: UIColor.darkGray, imageName: "feed_share_24x24_")
    /// 认证标识
    private lazy var verifyImageView: UIImageView = UIImageView(image: UIImage(named: "all_v_avatar_18x18_"))
    
    /// 子类重写该方法 进行扩展
    func setUpUI() -> Void
    {
        backgroundColor = UIColor.white
        let lineView = UIView()
        lineView.backgroundColor = UIColor(white: 0.9, alpha: 1)
        contentView.addSubview(lineView)
        contentView.addSubview(userIconImageView)
        contentView.addSubview(verifyImageView)
        contentView.addSubview(userNameLabel)
        contentView.addSubview(createTimeLabel)
        contentView.addSubview(contentLabel)
        
        lineView.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(5)
        }
        userIconImageView.snp.makeConstraints { (make) in
            make.top.equalTo(lineView.snp.bottom).offset(5)
            make.left.equalToSuperview().offset(10)
            make.size.equalTo(CGSize(width: 40, height: 40))
        }
        verifyImageView.snp.makeConstraints { (make) in
            make.centerX.equalTo(userIconImageView.snp.right).offset(-5)
            make.centerY.equalTo(userIconImageView.snp.bottom).offset(-5)
        }
        userNameLabel.snp.makeConstraints { (make) in
            make.centerY.equalTo(userIconImageView)
            make.left.equalTo(userIconImageView.snp.right).offset(10)
        }
        createTimeLabel.snp.makeConstraints { (make) in
            make.centerY.equalTo(userIconImageView)
            make.left.equalTo(userNameLabel.snp.right).offset(10)
        }
        contentLabel.snp.makeConstraints { (make) in
            make.top.equalTo(userIconImageView.snp.bottom).offset(10)
            make.left.equalToSuperview().offset(10)
            make.right.equalToSuperview().offset(-10)
            make.height.lessThanOrEqualTo(150)
        }
        
        let stackView = UIStackView(arrangedSubviews: [likeButton,commentButton,shareButton])
        stackView.distribution = .fillEqually
        contentView.addSubview(stackView)
        stackView.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(44)
        }
        let stackLineView = UIView()
        stackLineView.backgroundColor = UIColor.lightGray
        contentView.addSubview(stackLineView)
        stackLineView.snp.makeConstraints { (make) in
            make.top.equalTo(stackView)
            make.left.right.equalToSuperview()
            make.height.equalTo(0.5)
        }
    }
}

